﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CivilRegistry.ControlledModel;
using CivilRegistry.Common;

namespace CivilRegistry
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {

        public string SelectedMenu
        {
            get
            {
                return NavigationMenu.SelectedItem.Text;
            }
            set
            {
                foreach (MenuItem item in NavigationMenu.Items)
                {
                    if (item.Value == value)
                    {
                        item.Selected = true;
                        return;
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var user = SessionHandler.CurrentSession.User;

            if (user != null)
            {
                lblUserName.Text = "Welcome " + user.UserName + "!";
                lblUserName.Visible = true;
                lnkbtnChangePassword.Visible = true;
                lnkbtnLoginStatus.Text = "Log Out";

                if (user is Person)
                {
                    NavigationMenu.Items.RemoveAt(5);
                    NavigationMenu.Items.RemoveAt(5);
                }
                else if (user is Admin)
                {
                    NavigationMenu.Items.RemoveAt(5);
                    NavigationMenu.Items.RemoveAt(5);
                }
                else if (user is SuperAdmin)
                {
                    NavigationMenu.Items.RemoveAt(1);
                    NavigationMenu.Items.RemoveAt(1);
                    NavigationMenu.Items.RemoveAt(1);
                    NavigationMenu.Items.RemoveAt(2);
                }
                else if (user is Hospital)
                {
                    NavigationMenu.Items.RemoveAt(1);
                    NavigationMenu.Items.RemoveAt(1);
                    NavigationMenu.Items.RemoveAt(1);
                    NavigationMenu.Items.RemoveAt(3);
                }

                if (user.IsFirstLogin != null && user.IsFirstLogin.Value && this.Page.Request.FilePath != "/ChangePassword.aspx")
                {
                    Response.Redirect("~/ChangePassword.aspx");
                }
            }
            else
            {
                lnkbtnLoginStatus.Text = "Log In";
                NavigationMenu.Visible = false;
            }
        }

        protected void lnkbtnLoginStatus_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("~/Login.aspx");
        }

        protected void lnkbtnChangePassword_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ChangePassword.aspx");
        }
    }
}
